package com.cg.controller;

import com.cg.client.AppClient;
import com.cg.dto.Result;
import com.cg.dto.ResultEnum;
import com.cg.dto.ResultUtils;
import com.cg.entity.Patient;
import com.cg.utils.NumUtils;
import com.cg.utils.TokenUtils;
import com.cg.vo.IdCardVo;
import com.cg.vo.PatientListVo;
import com.cg.vo.PatientVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * (Patient)表控制层
 *
 * @author chengong
 * @since 2024-01-03 11:05:10
 */
@RestController
@RequestMapping("/patient")
public class PatientController {


    @Resource
    private AppClient appClient;

    /**
     * 根据身份证号获取生日，性别
     */
    @GetMapping("/queryBirthdayAndGender")
    public Result<IdCardVo> queryBirthdayAndGender(String idCard){
        //获取生日
        String birthday = NumUtils.getBirthdayByIdCard(idCard);
        if(birthday == null){
            return ResultUtils.returnResult(ResultEnum.FAIL_PARAM);
        }
        //获取性别
        Integer gender = NumUtils.getGenderByIdCard(idCard);
        IdCardVo idCardVo = new IdCardVo(birthday,gender);
        return ResultUtils.returnDataSuccess(idCardVo);
    }


    /**
     * 根据登录用户id-就诊人管理列表
     */
    @GetMapping("/patientListByPatientUserId")
    public Result<List<PatientListVo>> patientListByPhone(HttpServletRequest request)throws ParseException {
        String token = request.getHeader("token");
        Integer userId = TokenUtils.getUserIdByToken(token);
        return appClient.patientListByPhone(userId);
    }


    /**
     * 查询就诊人详情
     */
    @GetMapping("/queryPatientByCardNum")
    public Result<PatientVo> queryPatientByCardNum(String cardNum){
        return appClient.queryPatientByCardNum(cardNum);
    }


    /**
     * 添加就诊人
     */
    @PostMapping("/createPatient")
    public Result createPatient(HttpServletRequest request, @RequestBody Patient patient){
        String token = request.getHeader("token");
        Integer userId = TokenUtils.getUserIdByToken(token);
        patient.setPatientUserId(userId);
        return appClient.createPatient(patient);
    }


    /**
     * 根据token获取默认的就诊人信息
     */
    @PostMapping("/getPatientByToken")
    public Result<Patient> getPatientByToken(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userIdByToken = TokenUtils.getUserIdByToken(token);
        return appClient.getPatientByToken(userIdByToken);
    }


    /**
     * 根据登录用户的ID查询就诊人数量
     */
    @GetMapping("/queryCountByPatientUserId")
    public Result queryCountByPatientUserId(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = TokenUtils.getUserIdByToken(token);
        return appClient.queryCountByPatientUserId(userId);
    }


    /**
     * 生成条形码
     */
    @GetMapping("/barCode")
    public Result<String> barCode(String cardNum){
        return appClient.barCode(cardNum);
    }

    /**
     * 生成二维码
     */
    @GetMapping("/code")
    public Result<String> code(String cardNum){
        return appClient.code(cardNum);
    }
}

